package com.atguigu.flink05;

import com.atguigu.beans.WaterSensor;
import com.atguigu.func.WaterSensorMapFunction;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

/**
 * @author Felix
 * @date 2024/2/23
 * 该案例演示了窗口增量聚合 -- aggregate
 * 窗口中数据的类型、聚合类型以及向下游传递的类型可以不一致
 *  createAccumulator:初始化累加器    每个窗口执行一次
 *  add:聚合操作 每来一条数据执行一次
 *  getResult: 获取最终聚合结果     当窗口触发计算时候执行一次
 *  merge:只有会话窗口需要重写
 */
public class Flink05_window_agg {
    public static void main(String[] args) throws Exception {
        //TODO 1.指定环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        //TODO 2.从指定端口读取数据
        DataStreamSource<String> lineStrDS = env.socketTextStream("hadoop102", 8888);
        //TODO 3.对读取数据进行类型转换    String - WaterSensor
        SingleOutputStreamOperator<WaterSensor> wsDS = lineStrDS.map(new WaterSensorMapFunction());
        //TODO 4.按照传感器id进行分组
        KeyedStream<WaterSensor, String> keyedDS = wsDS.keyBy(WaterSensor::getId);
        //TODO 5.开窗   滚动处理时间窗口  窗口大小10s
        WindowedStream<WaterSensor, String, TimeWindow> windowDS
                = keyedDS.window(TumblingProcessingTimeWindows.of(Time.seconds(10)));
        //TODO 6.对窗口数据聚合计算  --- 增量聚合 aggregate
        SingleOutputStreamOperator<String> aggregateDS = windowDS.aggregate(
                new AggregateFunction<WaterSensor, Integer, String>() {
                    @Override
                    public Integer createAccumulator() {
                        System.out.println("~~~createAccumulator~~~");
                        return 0;
                    }

                    @Override
                    public Integer add(WaterSensor value, Integer accumulator) {
                        System.out.println("~~~add~~~");
                        return accumulator + value.vc;
                    }

                    @Override
                    public String getResult(Integer accumulator) {
                        System.out.println("~~~getResult~~~");
                        return accumulator.toString();
                    }

                    @Override
                    public Integer merge(Integer a, Integer b) {
                        System.out.println("~~~merge~~~");
                        return null;
                    }
                }
        );

        //TODO 7.将聚合结果进行输出
        aggregateDS.print();
        //TODO 8.提交作业
        env.execute();

    }
}
